package com.oss.validation;

import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;

import com.oss.model.Link;

/**
 * @author xueqiang.mi
 * @date 2010-2-9
 */
public class LinkValidator {

	public void validate(Link lnk, Errors errors) {

		if (!StringUtils.hasLength(lnk.getName())) {
			errors.rejectValue("name", "required", "required");
		}

		if (!StringUtils.hasLength(lnk.getUrl())) {
			errors.rejectValue("url", "required", "required");
		}

		if (lnk.getUrl().length() < 10
				|| !(lnk.getUrl().startsWith("http://") || lnk.getUrl()
						.startsWith("https://"))) {
			errors.rejectValue("url", "valid", "must be a valid URL");
		}

		if (!StringUtils.hasLength(lnk.getDescription())) {
			errors.rejectValue("description", "required", "required");
		}

		if (lnk.getDescription().length() > 2000) {
			errors.rejectValue("description", "valid",
					"must not longer than 2000 bytes");
		}
	}

	public static boolean validate(Link obj) {
		if (!StringUtils.hasLength(obj.getName())) {
			return false;
		}
		if (!StringUtils.hasLength(obj.getUrl())) {
			return false;
		}
		if (obj.getUrl().length() < 10
				|| !(obj.getUrl().startsWith("http://") || obj.getUrl()
						.startsWith("https://"))) {
			return false;
		}
		return true;
	}
}
